
package webprocessors;

import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 * This returns the html for students who have access to a given
 * group
 * @author Chris
 */
public class webprocessorGroupAccess extends webprocessor {
    public webprocessorGroupAccess() {}
    
    @Override
    public String process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        QueryStringHandler QSH = new QueryStringHandler(request.getQueryString());
        String user = getUserName(request);
        String groupid = QSH.getElement("grpid");
        String failed = QSH.getElement("failed");
        String failedMsg = "";
        if(failed != null) {
            failedMsg = "<h3>Failed to add student to group access, invalid student</h3>";
        }
        LinkedList<String> lst = new LinkedList<String>();
        
        if(groupid == null)
            return failedMsg + "No group was selected";
        
        if(!UserReader.getTeacherGroupAccess(user, groupid, lst)) {
            return "No one can access this group currently!";
        }
        
        String id;
        String output = failedMsg + "<table border=1><tr><td>Access to "+ groupid +"</td>";   
        while(!lst.isEmpty()) {
            id = lst.removeFirst();
            
            output += "<tr><td>" + id + "</td></tr>";
        }
        output += "</table>";
        
        return output;
    }

}
